package com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.Commodity;

import java.net.URI;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lugg on 2016/5/10.
 */
public class HomeFragmentRecommendAdapter extends BaseAdapter {

    Context context;
    List<Commodity> commodityList;
    public HomeFragmentRecommendAdapter(Context context,List<Commodity> commodityList){
        this.context = context;
        this.commodityList = commodityList;
    }

    @Override
    public int getCount() {
        return commodityList.size();
    }

    @Override
    public Object getItem(int position) {
        return commodityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = View.inflate(context,R.layout.list_item_fragment_home_recommed,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Commodity commodity= commodityList.get(position);
        Uri uri = Uri.parse(commodity.url);
        viewHolder.sdv_list_item_fragment_home_recommed_image.setImageURI(uri);
        viewHolder.tv_list_item_fragment_home_recommed_Company.setText(commodity.company);
        viewHolder.tv_list_item_fragment_home_recommed_name.setText(commodity.name);
        viewHolder.tv_list_item_fragment_home_recommed_price.setText(commodity.price);
        viewHolder.tv_list_item_fragment_home_recommed_specifications.setText(commodity.specifications);
        return convertView;
    }
    static class ViewHolder{
        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }

        @Bind(R.id.sdv_list_item_fragment_home_recommed_image)
        SimpleDraweeView sdv_list_item_fragment_home_recommed_image;

        @Bind(R.id.tv_list_item_fragment_home_recommed_name)
        TextView tv_list_item_fragment_home_recommed_name;

        @Bind(R.id.tv_list_item_fragment_home_recommed_specifications)
        TextView tv_list_item_fragment_home_recommed_specifications;

        @Bind(R.id.tv_list_item_fragment_home_recommed_price)
        TextView tv_list_item_fragment_home_recommed_price;

        @Bind(R.id.tv_list_item_fragment_home_recommed_Company)
        TextView tv_list_item_fragment_home_recommed_Company;
    }
}
